/************************************************************************
Purpose: Make elasticity table in the ETS paper
*************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_CLEAN/panel_plant-period-date-bid.dta", clear
drop if bid_status == "REJ"
drop if missing(bid_qty)
sort gpcb_id period date bid_id 


// There are 8433 bids overall in our data and 3120 offered in the first half of a compliance period. Of these bids, 345 were offered by plants that had only a single bid in the first half of a given plant-period, and which therefore do not contribute to the estimation of the key elasticity \beta_1.

preserve
keep if bid_day_norm <= 0.5
bysort gpcb_id period: gen num_bids = _N
keep if num_bids == 1
restore


drop cyc_max
drop bf_max
// drop scr_esp_max

gen cyc_max = (D_cyc == 1) & (D_bf == 0) & (D_scr == 0) & (D_esp == 0)
gen bf_max = (D_bf == 1) & (D_scr == 0) & (D_esp == 0)
gen scr_max = (D_scr == 1) & (D_esp == 0)
gen esp_max = (D_esp == 1)

gen tech_type = "."
replace tech_type = "cyc" if cyc_max == 1
replace tech_type = "bf" if bf_max == 1
replace tech_type = "scr" if scr_max == 1
replace tech_type = "esp" if esp_max == 1


 
tabulate tech_type

tabulate tech_type if bid_day_norm <= 0.5 
 
drop tech_type
// gen tech_type = -1
// replace tech_type = 1 if cyc_max == 1
// replace tech_type = 2 if bf_max == 1
// replace tech_type = 3 if scr_max == 1
// replace tech_type = 4 if esp_max == 1
	

*** Generate tech type
gen tech_type = -1
replace tech_type = 0 if cyc_max == 1 | bf_max == 1
replace tech_type = 1 if scr_esp_max == 1



*** Keep bids placed in the first half
keep if bid_day_norm <= 0.5

********************************************************************************
*** Run regressions
********************************************************************************

local price_var ln_bid_price
local holding_var ln_permit_holding_hyp


* 1) Control
* First run without dropping singletons to get the number of clusters.
eststo reg_1: reghdfe `price_var' `holding_var' ln_plant_total_heatoutput, noabsorb vce(cluster gpcb_id) keepsingletons
local N_plants_temp = e(N_clust)
* Run dropping singletons to get proper SEs, and add full plant count from first-run
eststo reg_1: reghdfe `price_var' `holding_var' ln_plant_total_heatoutput, noabsorb vce(cluster gpcb_id)
estadd local Period_FE ""
estadd local Plant_FE ""
estadd local Plant_Period_FE ""
estadd local N_Plants = `N_plants_temp'
estadd local space ""


* 2) Period FE + Control
eststo reg_2: reghdfe `price_var' `holding_var' ln_plant_total_heatoutput, a(period) vce(cluster gpcb_id) keepsingletons
local N_plants_temp = e(N_clust)
eststo reg_2: reghdfe `price_var' `holding_var' ln_plant_total_heatoutput, a(period) vce(cluster gpcb_id)
estadd local Period_FE "Yes"
estadd local Plant_FE ""
estadd local Plant_Period_FE ""
estadd local N_Plants = `N_plants_temp'
estadd local space ""

* 3) Period FE + Plant FE
reghdfe `price_var' `holding_var', a(period gpcb_id) vce(cluster gpcb_id) keepsingletons
local N_plants_temp = e(N_clust)
eststo reg_3: reghdfe `price_var' `holding_var', a(period gpcb_id) vce(cluster gpcb_id)
estadd local Period_FE "Yes"
estadd local Plant_FE "Yes"
estadd local Plant_Period_FE ""
estadd local N_Plants = `N_plants_temp'
estadd local space ""

* 4) Period x Plant FE

xtset plant_period
xtreg `price_var' `holding_var', fe
estimates store fixed
xtreg `price_var' `holding_var', re
// hausman fixed .
hausman fixed ., sigmamore

xtreg `price_var' `holding_var', fe vce(cluster gpcb_id)
xtreg `price_var' `holding_var', re vce(cluster gpcb_id)
xtoverid
local p_overid = r(jp)

// Test of overidentifying restrictions: fixed vs random effects
// Cross-section time-series model: xtreg re  robust cluster(gpcb_id)
// Sargan-Hansen statistic  46.213  Chi-sq(1)    P-value = 0.0000
* Rejection implies that the fixed effect model is more reasonable or preferred

reghdfe `price_var' `holding_var', a(i.plant_period) vce(cluster gpcb_id) keepsingletons
local N_plants_temp = e(N_clust)
eststo reg_4: reghdfe `price_var' `holding_var', a(i.plant_period) vce(cluster gpcb_id) 
estadd local p_overid = string(`p_overid', "%15.3f")
estadd local Period_FE ""
estadd local Plant_FE ""
estadd local Plant_Period_FE "Yes"
estadd local N_Plants = `N_plants_temp'
estadd local space ""


* 5) Period x Plant FE + Heterogeneity by tech type
keep if tech_type > -1

gen `holding_var'_0 = `holding_var' * (tech_type == 0)
gen `holding_var'_1 = `holding_var' * (tech_type == 1)
xtreg `price_var' `holding_var'_0 `holding_var'_1, fe vce(cluster gpcb_id)
xtreg `price_var' `holding_var'_0 `holding_var'_1, re vce(cluster gpcb_id)
xtoverid
local p_overid = r(jp)

reghdfe `price_var' c.`holding_var'#i.tech_type, a(i.plant_period) vce(cluster gpcb_id) keepsingletons
local N_plants_temp = e(N_clust)
eststo reg_5: reghdfe `price_var' c.`holding_var'#i.tech_type, a(i.plant_period) vce(cluster gpcb_id)
test 0.tech_type#c.ln_permit_holding_hyp = 1.tech_type#c.ln_permit_holding_hyp
estadd local p_obs = string(r(p), "%15.3f")
estadd local p_overid = string(`p_overid', "%15.3f")
estadd local Period_FE ""
estadd local Plant_FE ""
estadd local Plant_Period_FE "Yes"
estadd local N_Plants = `N_plants_temp'
estadd local space ""

 
********************************************************************************
*** Output the LaTeX Table
************************RADING********************************************************

esttab reg_1 reg_2 reg_3 reg_4 reg_5 ///
	using "$TRADING_TABS/Table_4.tex", replace ///
	b(3) se(3) ar2 booktabs varwidth(32) ///
	keep(ln_permit_holding_hyp 0.tech_type#c.ln_permit_holding_hyp 1.tech_type#c.ln_permit_holding_hyp ln_plant_total_heatoutput) ///
	order(ln_permit_holding_hyp 0.tech_type#c.ln_permit_holding_hyp 1.tech_type#c.ln_permit_holding_hyp ln_plant_total_heatoutput) ///
	coeflabel(ln_permit_holding_hyp "log(Emissions as bid)" ln_plant_total_heatoutput "log(Plant total heat output)" 0.tech_type#c.ln_permit_holding_hyp "log(Emissions as bid) $\times$ cyclone / bag filter" 1.tech_type#c.ln_permit_holding_hyp "log(Emissions as bid) $\times$ scrubber / ESP") ///
	mgroups("\shortstack{log(Bid price)}", pattern(1 0 0 0 0) ///	
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	nomtitles ///
	stats(Period_FE Plant_FE Plant_Period_FE space p_overid p_obs r2 N_Plants N_full , ///
		label("Period FE" "Plant FE" "Plant $\times$ Period FE" " " "p-val: $ H\textsubscript{0} $: No unobserved heterogeneity" "p-val: $ H\textsubscript{0} $: No observed heterogeneity" "R\textsuperscript{2}" "Plants" "Observations") ///
		fmt(%1s %1s %1s 0 %9.2f %9.2f %9.2f 0 0)) ///
	prefoot(" ") ///
	star(* 0.10 ** 0.05 *** 0.01) nonotes
